package com.nulldev.util.web.YouTubeExtractor.testing.tests;

import com.nulldev.util.internal.testing.Test;
import com.nulldev.util.internal.testing.TestResult;
import com.nulldev.util.web.YouTubeExtractor.signatures.ExtractedSignatureData;
import com.nulldev.util.web.YouTubeExtractor.signatures.extractor.Extractor;
import com.nulldev.util.web.YouTubeExtractor.signatures.extractors.BaseJSExtractor;
import com.nulldev.util.web.YouTubeExtractor.signatures.extractors.GraalBasedExtractor;
import com.nulldev.util.web.YouTubeExtractor.signatures.extractors.NashornBasedExtractor;
import com.nulldev.util.web.YouTubeExtractor.signatures.extractors.RhinoBasedExtractor;
import com.nulldev.util.web.YouTubeExtractor.signatures.extractors.SoftwareExtractor;

public class DeciphererTests {

	private static final String payload = "var Mx={FH:function(a){a.reverse()}," + "\"do\":function(a,b){var c=a[0];a[0]=a[b%a.length];a[b%a.length]=c},"
			+ "xK:function(a,b){a.splice(0,b)}};en=function(a){a=a.split(\"\");Mx[\"do\"](a,17);Mx.FH(a,61);Mx.xK(a,3);Mx[\"do\"](a,12);Mx.xK(a,1);Mx.FH(a,37);Mx[\"do\"](a,47);Mx.FH(a,6);return a.join(\"\")};";
	private static final String func = "var Mx={FH:function(a){a.reverse()},"
			+ "\"do\":function(a,b){var c=a[0];a[0]=a[b%a.length];a[b%a.length]=c},xK:function(a,b){a.splice(0,b)}};";
	public static final String input = "_46uRvDcixy9kZ0rFA5g6CMK8fRWCGWTNqOZ7mLNo2NiAEi=sTwdC-uJHzuK2olwcwNXvbW1knoBF6Ha63pRz6BL7aPAAIgRw8JQ0qOAhOAh";
	public static final String output = "AOq0QJ8wRgIhAPa7LB6zRp36aH6FBonk1WbvXNwcwlo2KuzHJu-CdwTsAiEAiN2oNLm7ZOqNTWGCWRf8KMC6g5_Fr0Zk9yxicDvRu64=";
	public static final ExtractedSignatureData ESD = new ExtractedSignatureData(payload, func, "en");

	public static class V8DeciphererTest extends Test<String> {

		private final Extractor ex;

		public V8DeciphererTest(final Extractor ex) {
			super(output);
			this.ex = ex;
		}

		public V8DeciphererTest(final TestResult ex) {
			super(ex);
			this.ex = null;
		}

		@Override
		public void test() throws Throwable {
			this.str_finish(ex.decrypt(input));
		}

	}

	public static class V8DeciphererSoftwareTest extends V8DeciphererTest {

		public V8DeciphererSoftwareTest() {
			super(new SoftwareExtractor(ESD));
		}

	}

	public static class V8DeciphererBaseJSTest extends V8DeciphererTest {

		public V8DeciphererBaseJSTest() {
			super(new BaseJSExtractor(ESD));
		}

	}

	public static class V8DeciphererNashhornTest extends V8DeciphererTest {

		public V8DeciphererNashhornTest() {
			super(new NashornBasedExtractor(ESD));
		}

		public V8DeciphererNashhornTest(final boolean noNashorn) {
			super(TestResult.DISABLED);
		}

	}

	public static class V8DeciphererRhinoTest extends V8DeciphererTest {

		public V8DeciphererRhinoTest() {
			super(new RhinoBasedExtractor(ESD));
		}

		public V8DeciphererRhinoTest(final boolean noNashorn) {
			super(TestResult.DISABLED);
		}

	}

	public static class V8DeciphererGraalTest extends V8DeciphererTest {

		public V8DeciphererGraalTest() {
			super(new GraalBasedExtractor(ESD));
		}

		public V8DeciphererGraalTest(final boolean noNashorn) {
			super(TestResult.DISABLED);
		}

	}
}
